<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '' : 'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem
                        :label="t('shop.advert.banner.image')"
                        type="image"
                        v-model="baTable.form.items!.image"
                        prop="image"
                        :input-attr="{
                            hideImagePlusOnOverLimit: true,
                        }"
                        tip="建议尺寸: 750 x 400"
                    />
                    <FormItem
                        :label="t('shop.advert.banner.status')"
                        type="switch"
                        v-model="baTable.form.items!.status"
                        prop="status"
                        :data="{
                            content: { '0': t('shop.advert.banner.status 0'), '1': t('shop.advert.banner.status 1') },
                            childrenAttr: { border: true },
                        }"
                        :placeholder="t('Please select field', { field: t('shop.advert.banner.status') })"
                        :input-attr="{
                            inactiveText: t('shop.advert.banner.status 0'),
                            activeText: t('shop.advert.banner.status 1'),
                            inlinePrompt: true,
                            style: {
                                '--el-switch-on-color': '#13ce66',
                                '--el-switch-off-color': '#ff4949',
                            },
                            width: 60,
                        }"
                    />
                    <FormItem
                        :label="t('shop.advert.banner.jump')"
                        type="switch"
                        v-model="baTable.form.items!.jump"
                        prop="jump"
                        :data="{
                            content: { '0': t('shop.advert.banner.jump 0'), '1': t('shop.advert.banner.jump 1') },
                            childrenAttr: { border: true },
                        }"
                        :placeholder="t('Please select field', { field: t('shop.advert.banner.jump') })"
                        :input-attr="{
                            inactiveText: t('shop.advert.banner.jump 0'),
                            activeText: t('shop.advert.banner.jump 1'),
                            inlinePrompt: true,
                            style: {
                                '--el-switch-on-color': '#13ce66',
                                '--el-switch-off-color': '#ff4949',
                            },
                            width: 60,
                        }"
                    />
<!--                    <FormItem-->
<!--                        v-if="baTable.form.items!.jump == 1"-->
<!--                        :label="t('shop.advert.banner.goods_id')"-->
<!--                        type="remoteSelect"-->
<!--                        v-model="baTable.form.items!.goods_id"-->
<!--                        prop="goods_id"-->
<!--                        :key="baTable.form.items!.goods_classify_id"-->
<!--                        :input-attr="{-->
<!--                            pk: 'goods.id',-->
<!--                            field: 'name',-->
<!--                            'remote-url': '/admin/shop.goods.Goods/index',-->
<!--                            disabled: baTable.form.items!.jump != '1',-->
<!--                        }"-->
<!--                        :placeholder="t('Please select field', { field: t('shop.advert.banner.goods_id') })"-->
<!--                        :attr="{-->
<!--                            blockHelp: t('shop.advert.banner.Jump to product details via carousel'),-->
<!--                        }"-->
<!--                    />-->
                    <FormItem  v-if="baTable.form.items!.jump == 1" :label="t('跳转视频')"  type="remoteSelect" v-model="baTable.form.items!.goods_id" prop="goods_id" :input-attr="{ pk: 'ba_task_task.id', field: 'name', remoteUrl: '/admin/task.Task/index' }" :placeholder="t('Please select field', { field: t('跳转视频') })" />

                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type { FormInstance, FormItemRule } from 'element-plus'
import { inject, reactive, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import { useConfig } from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import { buildValidatorData } from '/@/utils/validate'

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    image: [buildValidatorData({ name: 'required', message: t('shop.lang.fieldRequired', { field: t('shop.advert.banner.image') }) })],
    jump: [buildValidatorData({ name: 'required', message: t('shop.lang.fieldRequired', { field: t('shop.advert.banner.jump') }) })],
    goods_id: [buildValidatorData({ name: 'required', message: t('shop.lang.fieldRequired', { field: t('shop.advert.banner.goods_id') }) })],
    status: [buildValidatorData({ name: 'required', message: t('shop.lang.fieldRequired', { field: t('shop.advert.banner.status') }) })],
})
</script>

<style scoped lang="scss"></style>
